package com.usermanage.ServiceImpl;

import com.change.Service.AirSoftService;
import com.contract.Bean.SkuPriceTableDetail;
import com.contract.Common.DataBaseOpr;
import com.ordercore.Util.ResponseData.ResponseData;
import com.usermanage.Mapping.ContractRealTimePriceMapping;
import com.usermanage.Service.ContractRealTimePriceService;
import com.usermanage.Service.ContractService;
import com.usermanage.beanParam.ContractBean;
import com.usermanage.beanParam.ContractRealTimePriceBean;
import com.usermanage.dataDao.JpaDao;
import com.usermanage.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Slf4j
@Service("contractRealTimePriceService")
public class ContractRealTimePriceServiceImpl implements ContractRealTimePriceService {

    @Resource
    private ContractRealTimePriceMapping contractRealTimePriceMapping;
    @Resource
    private ContractService contractService;
    @Resource
    private JpaDao jpaDao;

    @Override
    @Transactional
    public JsonData save(ContractRealTimePriceBean bean) {
        contractRealTimePriceMapping.save(bean);
        return JsonData.success();

    }

    @Override
    @Transactional
    public List<ContractRealTimePriceBean> saveAll(List<ContractRealTimePriceBean> list) {
        return contractRealTimePriceMapping.saveAll(list);
    }

    @Override
    public JsonData syncSku(Map map, String type) {
        ResponseData responseData = null;
        if ("add".equals(type)) {
            SkuPriceTableDetail skuPriceTableDetail = new SkuPriceTableDetail();
            skuPriceTableDetail.setSku_brand(String.valueOf(map.get("sku_brand")));
            skuPriceTableDetail.setSku_official_price(String.valueOf(map.get("sku_official_price")));
            skuPriceTableDetail.setSku_cost_price(String.valueOf(map.get("sku_cost_price")));
            skuPriceTableDetail.setSku_brand(String.valueOf(map.get("sku_brand")));
            skuPriceTableDetail.setSku_unit(String.valueOf(map.get("sku_unit")));
            skuPriceTableDetail.setSku_name(String.valueOf(map.get("sku_name")));
            skuPriceTableDetail.setSku_code(String.valueOf(map.get("sku_code")));
            skuPriceTableDetail.setSku_lead_time(String.valueOf(map.get("sku_lead_time")));
            skuPriceTableDetail.setSku_system_code(String.valueOf(map.get("sku_system_code")));
            skuPriceTableDetail.setSku_contract_price("0");
            skuPriceTableDetail.setCreate_time(DateUtil.getNowTimeString());
            if (!"null".equals(String.valueOf(map.get("sku_pic_url")))) {
                skuPriceTableDetail.setSku_pic_url(String.valueOf(map.get("sku_pic_url")));
            }
            if (!"null".equals(String.valueOf(map.get("sku_model")))) {
                skuPriceTableDetail.setSku_model(String.valueOf(map.get("sku_model")));
            }
            if (!"null".equals(String.valueOf(map.get("sku_slug_set")))) {
                skuPriceTableDetail.setSku_slug_set(String.valueOf(map.get("sku_slug_set")));
            }
            if (!"null".equals(String.valueOf(map.get("sku_content")))) {
                skuPriceTableDetail.setSku_content(String.valueOf(map.get("sku_content")));
            }
            responseData = DataBaseOpr.baseDataCancalAdd(skuPriceTableDetail);
        } else if ("cancel".equals(type)) {
            responseData = DataBaseOpr.baseDataCancalSku(String.valueOf(map.get("sku_code")), StaticUtil.JDTARIFFID);
        } else if ("update".equals(type)) {
            responseData = DataBaseOpr.baseDataUpt(String.valueOf(map.get("sku_code")), StaticUtil.JDTARIFFID, String.valueOf(map.get("airSoft_id")));
        }
        return JsonData.success(responseData.getData());
    }

    @Override
    public List<ContractRealTimePriceBean> findAllList(String key, String contract_id, String page, String size, String type) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT * from u_contract_realtime_price WHERE 1 = 1  ");
        if (StringUtils.isNotBlank(key)) {
            buffer.append(" and concat(IFNULL(sku_code,''),IFNULL(sku_name,''),IFNULL(sku_brand,''),IFNULL(sku_unit,''),IFNULL(sku_slug_set,'')) like '%" + key + "%'");
        }
        buffer.append(" and contract_id = ").append(contract_id);
        if (StringUtils.isNotBlank(type)) {
            buffer.append(" and status = '").append(type).append("' ");
        }
        buffer.append(" order by id desc");
        if (Integer.valueOf(page) > 0) {
            buffer.append(" limit ").append((Integer.valueOf(page) - 1) * Integer.valueOf(size)).append(",").append(size);
        }
        List<ContractRealTimePriceBean> list = jpaDao.getListByPage(buffer.toString(), ContractRealTimePriceBean.class);

        return list;
    }

    @Override
    public List<ContractRealTimePriceBean> findListBySkuCode(String sku_code) {
//        StringBuffer buffer = new StringBuffer();
//        buffer.append("SELECT * from u_contract_realtime_price WHERE 1 = 1  ");
//        buffer.append(" and sku_code = ").append(sku_code);
        ContractRealTimePriceBean bean = new ContractRealTimePriceBean();
        bean.setSku_code(sku_code);
        Example<ContractRealTimePriceBean> example = Example.of(bean);
        List<ContractRealTimePriceBean> list = contractRealTimePriceMapping.findAll(example);
//        List<ContractRealTimePriceBean> list = contractRealTimePriceMapping.getListByPage(buffer.toString());
        return list;
    }

    @Override
    public int getAllListCount(String key, String contract_id, String type) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT count(1) from u_contract_realtime_price  WHERE 1 = 1  ");
        if (StringUtils.isNotBlank(key)) {
            buffer.append(" and concat(IFNULL(sku_code,''),IFNULL(sku_name,''),IFNULL(sku_brand,''),IFNULL(sku_unit,''),IFNULL(sku_slug_set,'')) like '%" + key + "%'");
        }
        if (StringUtils.isNotBlank(type)) {
            buffer.append(" and status = '").append(type).append("' ");
        }
        buffer.append(" and contract_id = ").append(contract_id);
        return jpaDao.getListCountByPage(buffer.toString());
    }

    public int getSkuBySkuCodeAndContractId(String sku_code, String contract_id, String type) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT count(1) from u_contract_realtime_price  WHERE 1 = 1  ");
        buffer.append(" and contract_id = ").append(contract_id);
        buffer.append(" and sku_code = '").append(sku_code).append("' ");
        buffer.append(" and status = '").append(type).append("' ");
        return jpaDao.getListCountByPage(buffer.toString());
    }

    public List<ContractRealTimePriceBean> getSkuBySkuCodeAndContractIdDetail(String sku_code, String contract_id) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT * from u_contract_realtime_price  WHERE 1 = 1  ");
        buffer.append(" and contract_id = ").append(contract_id);
        if (StringUtils.isNotBlank(sku_code)) {
            buffer.append(" and sku_code = '").append(sku_code).append("'");
        }
        buffer.append(" and status = '00'");
        return jpaDao.getListByPage(buffer.toString(), ContractRealTimePriceBean.class);
    }

    public List<ContractRealTimePriceBean> getSkuBySkuCodeAndContractIdDetailOfPm(String sku_code, String contract_id) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT * from u_contract_realtime_price  WHERE 1 = 1  ");
        buffer.append(" and contract_id = ").append(contract_id);
        if (StringUtils.isNotBlank(sku_code)) {
            buffer.append(" and sku_code = '").append(sku_code).append("'");
        }
        return jpaDao.getListByPage(buffer.toString(), ContractRealTimePriceBean.class);
    }

    public List<ContractRealTimePriceBean> getSkuBySkuCodeSetAndContractIdDetail(String sku_code_set, String contract_id, String page, String size, String key) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT * from u_contract_realtime_price  WHERE 1 = 1  ");
        buffer.append(" and contract_id = ").append(contract_id);
        buffer.append(" and status = '00'");
        if (StringUtils.isNotBlank(key)) {
            buffer.append(" and CONCAT (IFNULL(sku_code,''),IFNULL(sku_name,''),IFNULL(sku_slug_set,''),IFNULL(sku_brand,''),IFNULL(sku_unit,'')) LIKE '%").append(key).append("%'");
        }
        if (StringUtils.isNotBlank(sku_code_set)) {
            buffer.append(" and sku_code in (").append(sku_code_set).append(")");
        } else {
            buffer.append(" order by id desc limit ").append((Integer.valueOf(page) - 1) * Integer.valueOf(size)).append(",").append(size);
        }
        return jpaDao.getListByPage(buffer.toString(), ContractRealTimePriceBean.class);
    }

    public int getCountByContractId(String contract_id, String key) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT count(1) from u_contract_realtime_price  WHERE 1 = 1  ");
        buffer.append(" and contract_id = ").append(contract_id);
        buffer.append(" and status = '00'");
        if (StringUtils.isNotBlank(key)) {
            buffer.append(" and CONCAT (IFNULL(sku_code,''),IFNULL(sku_name,''),IFNULL(sku_slug_set,''),IFNULL(sku_brand,''),IFNULL(sku_unit,'')) LIKE '%").append(key).append("%'");
        }
        return jpaDao.getListCountByPage(buffer.toString());
    }

    @Override
    public JsonData update(ContractRealTimePriceBean bean) {
        return JsonData.success(contractRealTimePriceMapping.save(bean));
    }

    @Override
    public Optional<ContractRealTimePriceBean> findById(int id) {
        return contractRealTimePriceMapping.findById(id);
    }

}
